package com.zc.edu.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zc.edu.client.VodClient;
import com.zc.edu.entity.Video;
import com.zc.edu.service.VideoService;
import com.zc.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author cong
 * @since 2022-11-25
 */
@RestController
@RequestMapping("/eduService/video")
@CrossOrigin
public class VideoController {

	@Autowired
	private VideoService videoService;

	@Autowired
	private VodClient vodClient;
	// 添加小节
	@PostMapping("/addVideo")
	public R addVideo(@RequestBody Video video) {
		videoService.save(video);
		return R.ok();
	}

	// 删除小节
	@DeleteMapping("/deleteVideoById/{videoId}")
	public R deleteVideoById(@PathVariable("videoId") String videoId) {
		Video video = videoService.getById(videoId);
		String videoSourceId = video.getVideoSourceId();
		if (!StringUtils.isEmpty(videoSourceId)) {
			vodClient.deleteVideo(videoSourceId);
		}
		videoService.removeById(videoId);
		return R.ok();
	}

	// 根据id查询小节
	@GetMapping("/getVideoById/{videoId}")
	public R getVideoById(@PathVariable String videoId) {
		Video video = videoService.getById(videoId);
		return R.ok().data("video", video);
	}

	// 修改小节
	@PostMapping("/updateVideo")
	public R updateVideo(@RequestBody Video video) {
		videoService.updateById(video);
		return R.ok();
	}

}

